/******************************************************************************
* AIR SYSTEMS LABORATORY
* AEROSPACE ENGINEERING
* UNIVERISTY OF TEXAS AT AUSTIN
* UAV FLIGHT SOFTWARE 
* DESCRIPTION:
*   
*
* SOURCE: 
* Orignial: 08/14/2008  Jorge Alvarez joroge@gmail.com
* Modified: 03/20/2009	Sergio García-Nieto sergarro@isa.upv.es
* $Id: matrix.h 4 2009-07-21 17:12:11Z joroge@gmail.com $
*************************************************************************************/
/*-----------------------------------------------------------------------------
*	file:	matrix.h
*	desc:	matrix mathematics header file
*	by:	    ko shu pui, patrick
*	date:	24 nov 91	v0.1b
*	revi:   Rodney Teo and Jung Soon Jang
*	ref:
*       [1] Mary L.Boas, "Mathematical Methods in the Physical Sciene,"
*	John Wiley & Sons, 2nd Ed., 1983. Chap 3.
*
*-----------------------------------------------------------------------------*/

#ifndef _matrix_h
#define _matrix_h
#include <stdio.h>

/*-----------------------------------------------------------------------------
*	internal matrix structure
*------------------------------------------------------------------------------*/

typedef struct {
	int	row;
	int	col;
	}	MATHEAD;

typedef struct {
	MATHEAD	head;
	double	*matrix; // double
	}	MATBODY;

typedef	double	**MATRIX;// double pointer to array of pointers **a

//Macros to use 1 index instead of 0
#define	Mathead(a)	((MATHEAD *)((MATHEAD *)(a) - 1))
#define MatRow(a)	(Mathead(a)->row)
#define	MatCol(a)	(Mathead(a)->col)

/*
*----------------------------------------------------------------------------
*	matrix errors definitions
*----------------------------------------------------------------------------
*/
#define	MAT_MALLOC		1
#define MAT_FNOTOPEN	2
#define	MAT_FNOTGETMAT	3

/*
*----------------------------------------------------------------------------
*	matrices types
*----------------------------------------------------------------------------
*/
#define UNDEFINED		-1
#define ZERO_MATRIX		0
#define	UNIT_MATRIX		1
#define	ONES_MATRIX		2


/* prototypes of matrix package */

//Matrix Mathematical operations
MATRIX matrixAddition(MATRIX, MATRIX,MATRIX);
MATRIX matrixSubstraction(MATRIX,MATRIX,MATRIX);
MATRIX matrixMultiplication(MATRIX,MATRIX,MATRIX);
double matrixDiagonalMultiplication(MATRIX);
MATRIX matrixTranspose(MATRIX, MATRIX);
MATRIX matrixInverse(MATRIX,MATRIX);
MATRIX matrixLinearSolve(MATRIX, MATRIX,MATRIX);
MATRIX matrixBackSubstitution(MATRIX, MATRIX, MATRIX, MATRIX, int);
double matrixCofactor(MATRIX, int, int);
double matrixDeterminant(MATRIX);
double matrixMinor(MATRIX, int, int);
MATRIX matrixDurbin(MATRIX,MATRIX);
MATRIX matrixLsolveDurbin(MATRIX,MATRIX,MATRIX);
double matrixOneNorm(MATRIX X, int column);
double matrixDotProduct (MATRIX X,MATRIX Y);
MATRIX matrixScalarMultiplication(MATRIX X,double A,MATRIX C);
double matrixTrace(MATRIX X);
double matrixCondition(MATRIX X);
MATRIX matrixSkew(MATRIX X);
MATRIX matrixCrossProduct(MATRIX A,MATRIX B,MATRIX C);

//Matrix Manipulation
MATRIX matrixError(int);
MATRIX matrixCreation(int, int);
MATRIX matrixCreationType(int, int, int);
MATRIX matrixFill(MATRIX, int);
MATRIX matrixSubMatrix(MATRIX, int, int, MATRIX);
int matrixFree(MATRIX);
MATRIX matrixCopy(MATRIX, MATRIX);
MATRIX matrixCopyNegative(MATRIX, MATRIX);
MATRIX matrixColumnCopy(MATRIX, MATRIX, int, int);
MATRIX matrixSymToeplz(MATRIX, MATRIX);
int matrixLU(MATRIX, MATRIX);
MATRIX matrixSubCopy(MATRIX A, int row, int col, MATRIX B);
MATRIX matrixT321(double pitch, double roll, double yaw, MATRIX);
MATRIX matrixRound(MATRIX X, MATRIX C);



//Matrix display and saving
int getMatrixFromFile(MATRIX A, FILE *fp );
MATRIX matrixDumpStdo( MATRIX A);
MATRIX matrixDumpStdoString(MATRIX A,char *s);
MATRIX matrixFormatedDump(MATRIX A,FILE *fp);
MATRIX matrixFormatedDumpFile(MATRIX A,char *s,FILE *fp);

//matrix other multiplications
MATRIX mat_mymul(MATRIX A,MATRIX B, MATRIX C, short m);
MATRIX mat_mymul1(MATRIX A,MATRIX B, MATRIX C, short m);
MATRIX mat_mymul2(MATRIX A,MATRIX B, MATRIX C, short m);
MATRIX mat_mymul3(MATRIX A,MATRIX B, MATRIX C, short m);
MATRIX mat_mymul4(MATRIX A,MATRIX B, MATRIX C, short m);
MATRIX mat_mymul5(MATRIX A,MATRIX B, MATRIX C, short m);

#endif

